package lo53.bardoux.setupTools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import lo53.bardoux.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class Main extends Activity {

	private static final int portBurst = 8764; // Set as my Server listner port for test purposes
	
	private Button buttonMeasure;
	private Button buttonCancel;
	private FrameLayout frameLayout;
	private MapView mapView;
	
	private int mapId = 1;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setuptoolsmain);
        
        this.buttonMeasure = (Button) findViewById(R.id.buttonMeasureMain);
        this.buttonCancel = (Button) findViewById(R.id.buttonCancelMain);
        this.frameLayout = (FrameLayout) findViewById(R.id.frameLayoutMain);
        this.mapView = new MapView(this);
        this.frameLayout.addView(this.mapView);
    }
	
	public void clickButtonMeasure(View v) {
		
		DatagramSocket socket = null;
		
		// Burst of packets
		try {
			String burstMess = "Burst";
			socket = new DatagramSocket(Main.portBurst);
			InetAddress addr = InetAddress.getByName("192.168.1.255");
			DatagramPacket packet = new DatagramPacket(burstMess.getBytes(), burstMess.length(), addr, Main.portBurst);

			// We burst for 500 millisecondes
			long stopTime = System.currentTimeMillis()+500;
			while(System.currentTimeMillis() < stopTime){
				socket.send(packet);
			}
			
		} catch (Exception e1) {e1.printStackTrace();}
		finally{
			if (socket != null){
				socket.close();
			}
		}
		
		// Collect the data from the Preferences
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		String servAddress = prefs.getString("serverAddress", null);
		String servProjectName = prefs.getString("serverProjectName", null);
		String servCalibrationServlet = prefs.getString("serverCalibrationServlet", null);
		String servPort = prefs.getString("serverPort", "");
		
		String servletPath = "http://"+servAddress+":"+servPort+"/"+servProjectName+"/"+servCalibrationServlet+"?x="+this.mapView.getPointX()+"&y="+this.mapView.getPointY()+"&mid="+this.mapId;
		Toast.makeText(this.getApplicationContext(), servletPath , Toast.LENGTH_LONG).show();
		
		
		try {
			HttpClient client = new DefaultHttpClient();
	        HttpGet request = new HttpGet(servletPath);
	        HttpResponse response = client.execute(request);
	        HttpEntity entity = response.getEntity();
	        if(entity != null){
	            Toast.makeText(this.getApplicationContext(), "Server returned :\n"+EntityUtils.toString(entity) , Toast.LENGTH_LONG).show();
	        }
        }
        catch(Exception e){e.printStackTrace();}
	}
	
	public void clickButtonCancel(View v) {
		this.setEnableButtons(false);
	}
	
	public void setEnableButtons(boolean enabled){
		this.buttonMeasure.setEnabled(enabled);
		this.buttonCancel.setEnabled(enabled);
		if(!enabled)
			this.mapView.resetPoints();
	}
}
